package com.whfc.ms.api.cfg.service.factory;

import com.alibaba.fastjson.JSON;
import com.whfc.base.dto.AppWarnFreqConfigDTO;
import com.whfc.base.enums.AppModuleType;
import com.whfc.base.service.WarnConfigService;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.emp.dto.AppFaceGateConfigDTO;
import com.whfc.emp.dto.EmpValidFenceDTO;
import com.whfc.emp.service.AppEmpConfigService;
import com.whfc.emp.service.AppFaceGateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-07-30 17:16
 */
@Service(ConfigCenterServiceConstant.EMP_SERVICE)
public class ConfigCenterServiceEmpImpl implements ConfigCenterService {

    @DubboReference(interfaceClass = WarnConfigService.class, version = "1.0.0")
    private WarnConfigService warnConfigService;

    @DubboReference(interfaceClass = AppEmpConfigService.class, version = "1.0.0")
    private AppEmpConfigService appEmpConfigService;

    @DubboReference(interfaceClass = AppFaceGateService.class, version = "1.0.0")
    private AppFaceGateService appFaceGateService;

    @Override
    public List getConfigList(Integer deptId, String module) {
        if (ConfigCenterModuleConstant.EMP_FREQ.equals(module)) {
            return warnConfigService.getFreqConfigList(deptId, AppModuleType.EMP);
        } else if (ConfigCenterModuleConstant.EMP_GATE.equals(module)) {
            return appFaceGateService.getFaceGateList(deptId);
        } else if (ConfigCenterModuleConstant.EMP_FENCE.equals(module)) {
            return appEmpConfigService.getEmpValidFenceList(deptId);
        }
        return Collections.emptyList();
    }

    @Override
    public void setConfig(Integer deptId, String module, String dataStr) {
        if (ConfigCenterModuleConstant.EMP_FREQ.equals(module)) {
            List<AppWarnFreqConfigDTO> freqConfigList = JSON.parseArray(dataStr, AppWarnFreqConfigDTO.class);
            if (freqConfigList == null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "参数类型错误.");
            }
            warnConfigService.freqConfigure(deptId, AppModuleType.EMP, freqConfigList);
        } else if (ConfigCenterModuleConstant.EMP_GATE.equals(module)) {
            List<AppFaceGateConfigDTO> faceGateAppList = JSON.parseArray(dataStr, AppFaceGateConfigDTO.class);
            if (faceGateAppList == null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "参数类型错误.");
            }
            appFaceGateService.setFaceGateApp(deptId, faceGateAppList);
        } else if (ConfigCenterModuleConstant.EMP_FENCE.equals(module)) {
            List<EmpValidFenceDTO> validFenceList = JSON.parseArray(dataStr, EmpValidFenceDTO.class);
            if (validFenceList == null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "参数类型错误.");
            }
            appEmpConfigService.setEmpValidFence(deptId, validFenceList);
        }
    }

}

